//
//  NSDictionary+lvfj.m
//  lvfj
//
//  Created by lvfeijun on 2020/2/5.
//  Copyright © 2020 Goudu KuaiEx Technology(Shenzhen)Co.,ltd. All rights reserved.
//

#import "NSDictionary+lvfj.h"

@implementation NSDictionary (lvfj)
//通过取出字典的所有key值，利用sortedArrayUsingComparator进行降序排序
- (NSMutableArray *)sortToKeyUp:(BOOL)isUp{
 
    NSArray *allKeyArray = [self allKeys];
    NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSComparisonResult resuest = isUp?[obj1 compare:obj2]:[obj2 compare:obj1];
        return resuest;
    }];
    
    //通过排列的key值获取value
    NSMutableArray *valueArray = [NSMutableArray array];
    for (NSString *key in afterSortKeyArray) {
        [valueArray addObject:@{@"key":key,@"value":[self objectForKey:key]}];
    }
 
    return valueArray;
}



- (NSArray *)nk_ascendingComparedAllKeys
{
    NSArray *allKeys = [self keysSortedByValueUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        if ([obj1 integerValue] > [obj2 integerValue])
        {
            return (NSComparisonResult)NSOrderedAscending;
        }
        if ([obj1 integerValue] < [obj2 integerValue])
        {
            return (NSComparisonResult)NSOrderedDescending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
    
    return allKeys;
}

- (NSArray *)nk_descendingComparedAllKeys
{
    NSArray *allKeys = [self keysSortedByValueUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        if ([obj1 integerValue] < [obj2 integerValue])
        {
            return (NSComparisonResult)NSOrderedAscending;
        }
        if ([obj1 integerValue] > [obj2 integerValue])
        {
            return (NSComparisonResult)NSOrderedDescending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
    
    return allKeys;
}

@end
